/**
 * Description:
 * Class Name:TreeMapTest
 * Date:2022/3/6 16:20
 *
 * @author LTisme
 * @version JAVA17
 */
package 疯狂Java讲义.第8章_Java集合._8_6_增强的Map集合._8_6_5_SortedMap接口和TreeMap实现类;

import java.util.TreeMap;

class R implements Comparable{
    int count;
    public R(int count){
        this.count = count;
    }

    @Override
    public String toString() {
        return "R{" +
                "count=" + count +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        R r = (R) o;
        return count == r.count;
    }
    // 根据count属性值来判断两个对象的大小

    @Override
    public int compareTo(Object o) {
        var r = (R) o;
        return count > r.count ? 1 : count < r.count ? -1 : 0;
    }
}

public class TreeMapTest {
    public static void main(String[] args) {
        var tm = new TreeMap();
        tm.put(new R(3), "轻量级Java EE企业应用实战");
        tm.put(new R(-5), "疯狂Java讲义");
        tm.put(new R(9), "疯狂Android讲义");
        System.out.println(tm);

        // 返回该TreeMap的第一个Entry对象
        System.out.println(tm.firstEntry());
        System.out.println(tm.lastKey());
        System.out.println(tm.higherKey(new R(2)));
        System.out.println(tm.lowerEntry(new R(2)));
        System.out.println(tm.subMap(new R(-1), new R(4)));
    }
}
//{R{count=-5}=疯狂Java讲义, R{count=3}=轻量级Java EE企业应用实战, R{count=9}=疯狂Android讲义}
//R{count=-5}=疯狂Java讲义
//R{count=9}
//R{count=3}
//R{count=-5}=疯狂Java讲义
//{R{count=3}=轻量级Java EE企业应用实战}
